import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import * as nodeFetch from 'node-fetch';

configure({ adapter: new Adapter() });

global.Intl = require('intl');

global.fetch = nodeFetch.default;
global.Response = nodeFetch.Response;
global.Headers = nodeFetch.Headers;
global.Request = nodeFetch.Request;

if (!global.matchMedia) {
  global.matchMedia = () => ({
    matches: false,
    addListener: function() {},
    removeListener: function() {},
  });
}

if (!global.localStorage) {
  global.localStorage = {
    _data: {},
    setItem: function(id, val) {
      return (this._data[id] = String(val));
    },
    getItem: function(id) {
      return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
    },
    removeItem: function(id) {
      return delete this._data[id];
    },
    clear: function() {
      return (this._data = {});
    },
  };
}

if (!global.sessionStorage) {
  global.sessionStorage = {
    _data: {},
    setItem: function(id, val) {
      return (this._data[id] = String(val));
    },
    getItem: function(id) {
      return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
    },
    removeItem: function(id) {
      return delete this._data[id];
    },
    clear: function() {
      return (this._data = {});
    },
  };
}
